package com.example.dc.Service.Impl;

import com.example.dc.Service.QuestionService;
import com.example.dc.domain.*;
import com.example.dc.mapper.PersonalMapper;
import com.example.dc.mapper.QuestionMapper;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.dc.utils.SparkAPI;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private PersonalMapper personalMapper;

    @Override
    public Question getRandomQuestion(Integer userId) {
        return questionMapper.getRandomQuestion(userId);
    }



    @Override
    public Map<String,Object> matchAnwserKeyword(Integer personalQuestionId, Integer questionId, String question, String anwser, Integer userId){
        System.out.println("question:"+question);
        Reply reply=new Reply(questionId,anwser,userId);
        //1.插入用户回答表
        questionMapper.insertUserReply(reply);
        Integer replyId=reply.getId();
        //2.匹配关键词
        List<AnwserKeyword> anwserList= questionMapper.getAnwserKeyword(questionId);
        System.out.println("关键词表:"+anwserList.toString());
        //按关键词长度排序
        Collections.sort(anwserList,(AnwserKeyword o,AnwserKeyword v)->{
                return v.getContent().length()-o.getContent().length();
        });
        AnwserKeyword matchKeyword=null;//匹配到的关键词
        Integer anwserKeywordId=-1;
        Map<String,Object> result=new HashMap<>();
        result.put("reply","");
        result.put("score",0.0);
        //5.调用chatgpt返回回答给用户
        String content=AISparke(question,anwser);
//        3.1把问题改为已经提问
        personalMapper.updateQuestionTime(questionId,userId);

        //之后改为DFS关键词匹配
        for(AnwserKeyword ansKey:anwserList){
            System.out.println("匹配关键词中...");
            //匹配成功
            if(anwser.indexOf(ansKey.getContent())!=-1){
                LocalDateTime currentTime = LocalDateTime.now();
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                String formattedTime = currentTime.format(formatter);
                result.put("score",1.0*ansKey.getScore());
                System.out.println("匹配关键词ID："+ansKey.getId());
                System.out.println("成功匹配关键词："+ansKey.getContent());
                System.out.println("当前时间： " + formattedTime);
                System.out.println("关键词分值： " + ansKey.getScore());
                matchKeyword= ansKey;
                anwserKeywordId=ansKey.getId();
                //4.更新答案关键词匹配表
                questionMapper.updateAnwserKeyword(matchKeyword.getId());
                //6.更新用户打分表
                questionMapper.insertUserScore(personalQuestionId,ansKey.getId(),userId,replyId,content);
                break;
            }
        }
        //把chatgpt填入
        result.put("reply",content);
        //匹配失败
        if(matchKeyword==null){
            //后续处理
            System.out.println("匹配失败");
        }
        return result;
    }

    @Override
    public List<Question> getThreeStander() {
        return questionMapper.getThreeStander();
    }

    public String AISparke(String question,String anwser){
        JsonArray jsonArray=new JsonArray();
        JsonObject jsonObject=new JsonObject();
        jsonObject.addProperty("role","user");
        jsonObject.addProperty("content","你现在是我的健康顾问");
        jsonArray.add(jsonObject);

        jsonObject=new JsonObject();
        jsonObject.addProperty("role","assistant");
        jsonObject.addProperty("content",question);
        jsonArray.add(jsonObject);

        jsonObject=new JsonObject();
        jsonObject.addProperty("role","user");
        jsonObject.addProperty("content",anwser);
        jsonArray.add(jsonObject);

        System.out.println(jsonArray.toString());
        SparkAPI sparkAPI=new SparkAPI();

        String reply=sparkAPI.Test(jsonArray);
        return reply;
    }

    public Integer insertUserReply(Reply reply){
        return questionMapper.insertUserReply(reply);
    }

}
